package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.activity.WebBrowserActivity;
import com.jp.knowledge.adapter.FirstLoginGridAdapter;
import com.jp.knowledge.command.CommandInvoker;
import com.jp.knowledge.command.CommandReceiver;
import com.jp.knowledge.command.implement.DaKaiWangZhi;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.activity.HelpCatePageActivity;
import com.jp.knowledge.my.adapter.HelpCateListAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.HelpCateModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.JpDiver;

import org.xutils.common.util.DensityUtil;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 528787067@qq.com on 2017/8/1.
 */

public class FirstLoginDialog extends Dialog implements ResultSubscriber.OnResultListener {

	private static final int GRID_CODE = 0x01;
	private static final int LIST_CODE = 0x02;

	private Context mContext;
	private View contentView;
	private ImageView close;
	private RecyclerView grid;
	private RecyclerView list;
	private FirstLoginGridAdapter gridAdapter;
	private HelpCateListAdapter listAdapter;
	private List<HelpCateModel> gridDatas;
	private List<HelpCateModel> listDatas;

	public FirstLoginDialog(Context context) {
		super(context, R.style.custom_idalgo);
		mContext = context;
		setCanceledOnTouchOutside(false);
		setCancelable(false);
		contentView = View.inflate(context, R.layout.dialog_first_login, null);
		close = (ImageView) contentView.findViewById(R.id.close);
		grid = (RecyclerView) contentView.findViewById(R.id.first_login_grid);
		list = (RecyclerView) contentView.findViewById(R.id.first_login_list);
		grid.setHasFixedSize(true);
		list.setHasFixedSize(true);
		grid.addItemDecoration(new JpDiver(context));
		list.addItemDecoration(new JpDiver(context));
		grid.setLayoutManager(new GridLayoutManager(context, 3));
		list.setLayoutManager(new LinearLayoutManager(context));
		gridAdapter = new FirstLoginGridAdapter(context, gridDatas);
		listAdapter = new HelpCateListAdapter(context, listDatas, true);
		grid.setAdapter(gridAdapter);
		list.setAdapter(listAdapter);
		setContentView(contentView);
		WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
		layoutParams.width = (int) (DensityUtil.getScreenWidth() * 0.9);
		layoutParams.height = (int) (DensityUtil.getScreenHeight() * 0.8);
		gridAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
			@Override
			public void itemSelect(int position) {
				if(gridDatas != null || gridDatas.size() > position)
					itemClick(gridDatas.get(position));
			}
		});
		listAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
			@Override
			public void itemSelect(int position) {
				if(listDatas != null || listDatas.size() > position)
					itemClick(listDatas.get(position));
			}
		});
		close.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(clickBtnClickListener != null)
					clickBtnClickListener.onClickBtnClick();
				else
					cancel();
			}
		});
	}

	private void itemClick(HelpCateModel data){
		if(data == null)
			return;
//		mContext.startActivity(new Intent(mContext, HelpCatePageActivity.class)
//				.putExtra("title", data.getTitle())
//				.putExtra("pageId", data.getPageId()));
		WebBrowserActivity.openWebActivity(mContext, data.getUrl());
	}

	@Override
	public void show() {
		if(gridDatas == null || listDatas == null)
			getData();
		else
			super.show();
	}

	private void getData(){
		getGridData();
		getListData();
		if(dataGetListener != null)
			dataGetListener.onStart();
	}

	private void getGridData(){
		JsonObject param = new JsonObject();
		param.addProperty("id", "9f8209ca-827d-3a3a-da72-f51a1e4eddde");
		HttpHelper.getInstance(mContext).helpCateList(param, GRID_CODE, this);
	}

	private void getListData(){
		JsonObject param = new JsonObject();
		param.addProperty("id", "ec3b8780-81f7-02a0-6df0-4793c6651e7c");
		HttpHelper.getInstance(mContext).helpCateList(param, LIST_CODE, this);
	}

	@Override
	public void onStart(int requestType) {}

	@Override
	public void onCompleted(int requestType) {
		if(gridDatas != null && listDatas != null){
			super.show();
			if(dataGetListener != null)
				dataGetListener.onFinish();
		}
	}

	@Override
	public void onError(int requestType) {
		onCompleted(requestType);
	}

	@Override
	public void onNext(IModel data, int requestType) {
		if(data.getErrcode() != 0){
			ToasUtil.toast(mContext, data.getMessage());
			onError(requestType);
		} else if(requestType == GRID_CODE){
			gridDatas = data.getList(HelpCateModel.class);
			gridAdapter.updateAdapter(gridDatas);
		} else if(requestType == LIST_CODE){
			listDatas = data.getList(HelpCateModel.class);
			listAdapter.updateAdapter(listDatas);
		}
	}


	private DataGetListener dataGetListener;

	public void setDataGetListener(DataGetListener dataGetListener){
		this.dataGetListener = dataGetListener;
	}

	public interface DataGetListener{
		void onStart();
		void onFinish();
	}

	private ClickBtnClickListener clickBtnClickListener;

	public void setClickBtnClickListener(ClickBtnClickListener clickBtnClickListener){
		this.clickBtnClickListener = clickBtnClickListener;
	}

	public interface ClickBtnClickListener{
		void onClickBtnClick();
	}
}
